//
//  AFHTTPRequestOperation.m
//  nbUtil
//
//  Created by 相颖 on 16/7/9.
//  Copyright © 2016年 相颖. All rights reserved.
//

#import "AFHTTPRequestOperation.h"
#import "NSString+TPCategory.h"

@interface AFHTTPRequestOperation ()

@end

@implementation AFHTTPRequestOperation

- (instancetype)initWithTask:(NSURLSessionTask*)task{
    self = [self init];
    if (self) {
        self.sessionTask = task;
    }
    return self;
}

- (instancetype)init{
    self = [super init];
    self.failedTime = 0;
    return self;
}

- (void)dealloc{

}

- (void)cancel{
    if ([self.sessionTask respondsToSelector:@selector(cancelByProducingResumeData:)]) {
        [(NSURLSessionDownloadTask*)self.sessionTask cancelByProducingResumeData:^(NSData * _Nullable resumeData) {
            
        }];
    }else{
        [self.sessionTask cancel];
    }
}

- (BOOL)isExecuting{
    return self.sessionTask.state == NSURLSessionTaskStateRunning;
}

//- (BOOL)isCancelled{
//    return self.sessionTask.state == NSURLSessionTaskStateCanceling;
//}

- (NSURLRequest*)request{
    return self.sessionTask.originalRequest;
}

- (void)start{
    [self.sessionTask resume];
}

- (void)suspend{
    [self.sessionTask suspend];
}

- (NSHTTPURLResponse*)response{
    return (NSHTTPURLResponse*)self.sessionTask.response;
}

+ (NSString*)lastModefied:(NSURL*)url{
    return [[NSUserDefaults standardUserDefaults] objectForKey:[url.absoluteString stringFromMD5]];
}

+ (void)setlastModefied:(NSString*)lastModefied forKey:(NSURL*)url{
    if (lastModefied && url) {
        [[NSUserDefaults standardUserDefaults] setObject:lastModefied forKey:[url.absoluteString stringFromMD5]];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
}

+ (void)removelastModefied:(NSURL*)url{
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:[url.absoluteString stringFromMD5]];
}

//#pragma mark - request
//+ (AFHTTPRequestOperation *)request:(NSString *)url method:(NSString *)method params:(NSDictionary *)params complete:(void (^)(AFHTTPRequestOperation *, id))complete {
//    return request(url, [method lowercaseString] ?: @"get", AFJSONParameterEncoding, params, complete);
//}
//#pragma mark - download
//+ (AFHTTPRequestOperation *)download:(NSString *)url destPath:(NSString *)destPath progress:(void(^)(NSString *aUrl,long long currentprogress,long long totalprogress))progress complete:(void(^)(AFHTTPRequestOperation *operation, id data))complete {
//    return downloadFile(url, destPath, progress, complete);
//}

@end
